<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddRepairVehicle" type="primary" style="float:right;margin-top:13px;">录入维修车辆</el-button>
  </div>
  <!-- 维修车辆信息弹窗 -->
  <el-dialog
      :title="repairVehicleDialogTitle" v-model="repairVehicleDialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input v-model="saveRepairForm.username" readonly disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择" v-model="saveRepairForm.vehicleId">
              <el-option v-for="v in vehicleArr" :label="v.license" :value="v.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveRepairForm.item">
              <el-option v-for="i in repairItemArr" :label="i.label" :value="i.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveRepairForm.addr">
              <el-option v-for="a in repairAddrArr" :label="a.label" :value="a.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="saveRepairForm.material">
              <el-option v-for="m in repairMaterialArr" :label="m.label" :value="m.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入维修费用" v-model="saveRepairForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker v-model="saveRepairForm.repairTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveRepairForm.status">
              <el-radio label="维修中" value="1"></el-radio>
              <el-radio label="已完成" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepair">保存</el-button>
    </template>
  </el-dialog>
  <!-- 维修信息搜索卡片-->
  <el-card style="margin: 20px;">
    <el-form inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" v-model="searchRepairForm.license" @keydown.enter="loadRepair"></el-input>
      </el-form-item>
      <el-form-item label="送修人员">
        <el-input placeholder="请输入送修人姓名" v-model="searchRepairForm.username" @keydown.enter="loadRepair"></el-input>
      </el-form-item>
      <el-form-item label="维修状态">
        <el-select placeholder="请选择维修状态" style="width: 200px;" v-model="searchRepairForm.status" @change="loadRepair">
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepair">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 维修车辆信息表格 -->
  <el-card style="margin: 20px;">
    <el-table :data="repairVehicleTableData">
      <el-table-column type="index" width="80px" align="center"></el-table-column>
      <el-table-column prop="vehicleId" label="维修车辆" align="center" :formatter="repairVehicleFormatter"></el-table-column>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="repairItemFormatter"></el-table-column>
      <el-table-column prop="username" label="送修人员" align="center"></el-table-column>
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="repairAddrFormatter"></el-table-column>
      <el-table-column prop="repairTime" label="维修时间" align="center"></el-table-column>
      <el-table-column prop="fee" label="维修费用" align="center"></el-table-column>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="repairMaterialFormatter"></el-table-column>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="repairStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editRepair(scope.row.id)">编辑</el-button>
          <el-button link type="warning" @click="deleteRepair(scope.row.id,scope.row.vehicleId,scope.row.status)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const repairVehicleDialogTitle = ref('');
const repairVehicleDialogVisible = ref(false);
const user = ref(getUser());
const vehicleArr = ref([]);
const repairVehicleTableData = ref([]);
const repairMaterialArr = ref([]);
const repairItemArr = ref([]);
const repairAddrArr = ref([]);
const saveRepairForm = ref({
  username:user.value.username,vehicleId:'',item:'',addr:'',material:'',fee:'',repairTime:'',status:'',userId:user.value.id
});
onMounted(()=>{
  axios.get(BASE_URL+'/v1/vehicle/select')
      .then((response)=>{
        if (response.data.code == 2000){
          vehicleArr.value = response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
  loadDictOption(repairMaterialArr,'repair_material');
  loadDictOption(repairItemArr,'repair_item');
  loadDictOption(repairAddrArr,'repair_addr');
  loadRepair();
})
const beforeAddRepairVehicle = ()=>{
  repairVehicleDialogTitle.value = '录入维修车辆';
  repairVehicleDialogVisible.value = true;
  saveRepairForm.value.status = '1';
};
const searchRepairForm = ref({license:'',username:'',status:''});
const loadRepair = ()=>{
  let data = qs.stringify(searchRepairForm.value);
  axios.get(BASE_URL+'/v1/repair/select?'+data)
      .then((response)=>{
        if (response.data.code == 2000){
          repairVehicleTableData.value = response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
const repairVehicleFormatter = (row,column,cellValue,index)=>{
  if (!vehicleArr.value){
    return cellValue;
  }
  for (let item of vehicleArr.value){
    if (item.id == cellValue){
      cellValue = item.license;
    }
  }
  return cellValue;
}
const repairItemFormatter = (row,column,cellValue,index)=>{
  if (!repairItemArr.value){
    return cellValue;
  }
  for (let item of repairItemArr.value){
    if (item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairAddrFormatter = (row,column,cellValue,index)=>{
  if (!repairAddrArr.value){
    return cellValue;
  }
  for (let item of repairAddrArr.value){
    if (item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairMaterialFormatter = (row,column,cellValue,index)=>{
  if (!repairMaterialArr.value){
    return cellValue;
  }
  for (let item of repairMaterialArr.value){
    if (item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue == 0){
    cellValue = '已完成';
  }else if (cellValue == 1){
    cellValue = '维修中';
  }
  return cellValue;
}
const resetSearch = ()=>{
  searchRepairForm.value = {};
  loadRepair();
}
const saveRepair = ()=>{
  let data = qs.stringify(saveRepairForm.value);
  axios.post(BASE_URL+'/v1/repair/save',data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('维修单保存成功！');
          saveRepairForm.value = {username:user.value.username,vehicleId:'',item:'',addr:'',material:'',fee:'',repairTime:'',status:'',userId:user.value.id};
          repairVehicleDialogVisible.value = false;
          loadRepair();
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
const handleClose = ()=>{
  if (confirm('确定关闭此弹窗吗？')){
    saveRepairForm.value = {username:user.value.username,vehicleId:'',item:'',addr:'',material:'',fee:'',repairTime:'',status:'',userId:user.value.id};
    repairVehicleDialogVisible.value = false;
    loadRepair();
  }
}
const editRepair = (id)=>{
  repairVehicleDialogTitle.value = '编辑维修车辆';
  repairVehicleDialogVisible.value = true;
  axios.get(BASE_URL+'/v1/repair/select?id='+id)
      .then((response)=>{
        if (response.data.code == 2000){
          saveRepairForm.value = response.data.data[0];
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
const deleteRepair = (id,vehicleId,status)=>{
  if (confirm('删除维修单前请确认车辆维修状态！！！')){
    axios.post(BASE_URL+'/v1/repair/delete/'+id+'/'+vehicleId+'/'+status)
        .then((response)=>{
          if (response.data.code == 2000){
            ElMessage.success('维修单删除成功！');
            loadRepair();
          }else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
</script>

<style scoped>

</style>